import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { Message } from 'element-ui'
import { getCookie } from '../utils/cookie'
import store from '@/store'
import jwtParser from 'jwt-decode'

Vue.use(Router)

// 路由表
export const constantRouterMap = [
    {
        path: '/',
        component: () => import('@/views/common'),
        children: [
            { path: '', component: () => import('@/views/common/home') },
            { path: 'login', component: () => import('@/views/common/login') },
            { path: 'register', component: () => import('@/views/common/register') },
            { path: 'back', component: () => import('@/views/common/back') },
            {
                path: 'area',
                component: () => import('@/views/common/area'),
                children: [
                    { path: '', component: () => import('@/views/common/area/global') },
                    { path: ':name', component: () => import('@/views/common/area/areaPost') },
                    { path: ':name/:id(\\d+)', component: () => import('@/views/common/area/areaPost/post') },
                    { path: ':name/publish', component: () => import('@/views/common/area/areaPost/publish') }
                ]
            },
            {
                path: 'label',
                component: () => import('@/views/common/label'),
                children: [
                    { path: '', component: () => import('@/views/common/label/global') }
                ]
            },
            {
                path: 'push',
                component: () => import('@/views/common/push'),
                children: [
                    { path: '', component: () => import('@/views/common/push/global') },
                    { path: ':id(\\d+)', component: () => import('@/views/common/push/reply') }
                ]
            },
            {
                path: 'user/:type',
                component: () => import('@/views/common/userInfo'),
                children: [
                    {
                        path: ':id',
                        component: () => import('@/views/common/userInfo')
                    }
                ]
            },
            {
                path: 'attention/:type/:id(\\d+)',
                component: () => import('@/views/common/attention')
            },
            {
                path: 'search/:page(\\d+)/:size(\\d+)',
                component: () => import('@/views/common/search')
            },
            {
                path: 'search/reply/:id(\\d+)',
                component: () => import('@/views/common/search/reply')
            }
        ]
    },
    {
        path: '/admin',
        component: () => import('@/views/admin'),
        children: [
            {
                path: '',
                component: () => import('@/views/admin/home')
            },
            {
                path: 'user',
                component: () => import('@/views/admin/adminMenu/userOperate'),
                redirect: 'user/account',
                children: [
                    {
                        path: 'account',
                        component: () => import('@/views/admin/adminMenu/userOperate/accountManagement')
                    },
                    {
                        path: 'user_role',
                        component: () => import('@/views/admin/adminMenu/userOperate/userRoleManagement')
                    },
                    {
                        path: 'role',
                        component: () => import('@/views/admin/adminMenu/userOperate/roleManagement')
                    },
                    {
                        path: 'role_permission',
                        component: () => import('@/views/admin/adminMenu/userOperate/rolePermissionManagement')
                    },
                    {
                        path: 'permission',
                        component: () => import('@/views/admin/adminMenu/userOperate/permissionManagement')
                    }
                ]
            },
            {
                path: 'area',
                component: () => import('@/views/admin/adminMenu/areaOperate')
            },
            {
                path: 'label',
                component: () => import('@/views/admin/adminMenu/labelOperate')
            },
            {
                path: 'post',
                component: () => import('@/views/admin/adminMenu/postOperate'),
                redirect: 'post/post',
                children: [
                    {
                        path: 'post',
                        component: () => import('@/views/admin/adminMenu/postOperate/postManagement')
                    },
                    {
                        path: 'reply',
                        component: () => import('@/views/admin/adminMenu/postOperate/replyManagement')
                    }
                ]
            }
        ]
    }
]
// 白名单
export const whiteList = ['/', '/area', '/area/:name', '/area/:name/:id(\\d+)', '/home', '/login', '/register', '/back', '/label']

let router = new Router({
    routes: constantRouterMap
})

NProgress.configure({ showSpinner: false })

router.beforeEach((to, from, next) => {
    NProgress.start()
    let token = getCookie('token')
    // 如果具有Token
    if (token) {
        // 无需登录和注册
        if (to.path.startsWith('/login') || to.path.startsWith('/register')) {
            next(from.path)
            Message.warning('您已经登录了哦')
            NProgress.done()
            // 当从普通页面跳到管理页面时
        } else if ((!from.path.startsWith('/admin')) && to.path.startsWith('/admin')) {
            // 如果有管理员角色
            let user = store.getters['user/getUser']
            if (!user) {
                user = jwtParser(token)
            }
            if (user.hasAdminRole) {
                next()
            } else {
                next(from.path)
                Message.warning('您并没有管理员角色，无法进行管理操作哦')
                NProgress.done()
            }
            // 其它情况
        } else {
            next()
        }
    } else {
        let path = to.matched.map(value => value.path).pop()
        if (path.length > 1) {
            path = path.endsWith('/') ? path.substr(0, path.length - 1) : path
        }
        // 如果白名单中存在
        if (whiteList.includes(path)) {
            next()
        } else {
            // 否则全部重定向到登录页
            next(`/login?redirect=${to.path}`)
            NProgress.done()
        }
    }
})

router.afterEach(() => {
    NProgress.done()
})

export default router
